package com.gudaichaoren.spider.controller;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.enums.ApiErrorCode;
import com.gudaichaoren.spider.algorithm.LotteriesShuangseAlgorithm;
import com.gudaichaoren.spider.annotations.AllowAnonymous;
import com.gudaichaoren.spider.constant.Constants;
import com.gudaichaoren.spider.entity.LotteriesShuangseCalculate;
import com.gudaichaoren.spider.entity.LotteriesShuangseResults;
import com.gudaichaoren.spider.entity.User;
import com.gudaichaoren.spider.model.request.QueryWinningRequest;
import com.gudaichaoren.spider.model.response.LotteriesShuangseCalculateRes;
import com.gudaichaoren.spider.pageprocesser.LotteriesShuangsePageProcesser;
import com.gudaichaoren.spider.service.LotteriesShuangseCalculateService;
import com.gudaichaoren.spider.utils.RedisUtil;
import com.sun.org.apache.bcel.internal.generic.RETURN;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@RestController
@RequestMapping("/spider")
@Slf4j
public class ResultController {
    @Autowired
    private LotteriesShuangsePageProcesser lotteriesShuangsePageProcesser;

    @Autowired
    private LotteriesShuangseCalculateService lotteriesShuangseCalculateService;

    @Autowired
    private RedisUtil redisUtil;

    @Autowired
    private LotteriesShuangseAlgorithm lotteriesShuangseAlgorithm;

    @GetMapping("/Crawling")
    @AllowAnonymous
    public R Crawling() {
        lotteriesShuangsePageProcesser.Crawling();
        return new R(ApiErrorCode.SUCCESS);
    }

    @GetMapping("/getHtml")
    @AllowAnonymous
    public R<String> getHtml() {
        String s = redisUtil.get(Constants.RedisConstant.INTEGRAL_HTML);
        return R.ok(s);
    }

    @GetMapping("/getCalculateData")
    @AllowAnonymous
    public R<List<LotteriesShuangseCalculate>> getCalculateData() {
        List<LotteriesShuangseCalculate> list = lotteriesShuangseCalculateService.list(new QueryWrapper<LotteriesShuangseCalculate>());
        return R.ok(list);
    }

    @GetMapping("/Calculate")
    @AllowAnonymous
    public R Calculate() {
        lotteriesShuangseAlgorithm.Calculate();
        return new R(ApiErrorCode.SUCCESS);
    }

    @PostMapping("/queryWinning")
    @AllowAnonymous
    public R<List<LotteriesShuangseCalculateRes>> queryWinning(@RequestBody QueryWinningRequest queryWinningRequest) {
        return R.ok(lotteriesShuangseAlgorithm.queryWinning(queryWinningRequest));
    }

    @GetMapping("test-freemarker")
    @AllowAnonymous
    public ModelAndView index(HttpServletRequest request, HttpServletResponse response) {
        User user = new User();
        user.setUserName("张三");
        ModelAndView mv = new ModelAndView();
            mv.setViewName("page/index");
            mv.addObject(user);
        return mv;
    }

    @GetMapping("test-freemarker1")
    @AllowAnonymous
    public void index1(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String s = redisUtil.get(Constants.RedisConstant.INTEGRAL_HTML);
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out=response.getWriter();
        out.println(s);
    }
}
